package Sdo.util;

import java.io.File;

import org.apache.log4j.Logger;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import Sdo.pojo.History;

public class editPane{

	private static Logger logger = Logger.getLogger(editPane.class);  


	//设置图标
			
		ImageView iconimage = new ImageView(new Image("/icon.png", 20, 20, false, false));
	
		
	private History history;
	private TextField name;
	private TextField host;
	private TextField port;
	private TextField username;
	private PasswordField password;
	
	
	private ComboBox<String> proxyType;
	private TextField proxyIp;
	private TextField proxyPort;
	private TextField proxyUser;
	private PasswordField proxyPassword;
	
	
	private Button pkey;
	
	//private final Desktop desktop = Desktop.getDesktop();
	
	final FileChooser fileChooser = new FileChooser();
	

	 
	public editPane(History history) {
		// TODO Auto-generated constructor stub
    	  this.history = history;
    	
	}


	public GridPane GetAddPane() {

		// TODO Auto-generated method stub
		
		logger.debug("开始编辑");
    	GridPane grid = new GridPane();
    	grid.setAlignment(Pos.CENTER);
    	grid.setHgap(5);
    	grid.setVgap(10);
    	grid.setPadding(new Insets(10, 10, 10, 10));// 设置填充, Insets实例设置矩形区域的四边的一组内偏移量


    	name = new TextField(history.getName());
		name.setPromptText("名称");
		
    	
    	host = new TextField(history.getIp());
		host.setPromptText("IP");
    	
    	
    	port = new TextField(String.valueOf(history.getPort()));
		port.setPromptText("端口");
		
    	username = new TextField(history.getUsername());
		username.setPromptText("用户名");
		
    	password = new PasswordField();
    	password.setText(history.getPassword());
		password.setPromptText("密码");

		
    	String ppk="选择密钥";
    	if(!"".equals(history.getPkey())) {
    		ppk=history.getPkey();
    	}
    	pkey = new Button(ppk);
    	
    	
    	proxyType = new ComboBox<>();
    	proxyType.getItems().add(0, "代理类型");
    	proxyType.getItems().add(1, "HTTP");
    	proxyType.getItems().add(2, "SOCKS5");
    	proxyType.getItems().add(3, "SOCKS4");
    	 
    	
    	
    	

    	proxyIp = new TextField(history.getProxyIp());
    	proxyIp.setPromptText("代理IP");
    	
    	proxyPort = new TextField(history.getProxyPort());
    	proxyPort.setPromptText("代理端口");
    	
    	proxyUser = new TextField(history.getProxyUser());
    	proxyUser.setPromptText("代理用户");
    	
    	proxyPassword = new PasswordField();
    	proxyPassword.setText(history.getProxyPassword());
    	proxyPassword.setPromptText("代理密码");
    	
    	VBox vbox = new VBox(8);
    	vbox.getChildren().addAll(proxyIp,proxyPort,proxyUser,proxyPassword);
    	
    	

    	Button confirm = new Button("确定");
    	Button cancle = new Button("取消");
    	
    	grid.add(name, 1, 1);
    	grid.add(host, 1, 2);
    	grid.add(port, 1, 3);
    	grid.add(username, 1, 4);
    	grid.add(password, 1, 5);
    	grid.add(pkey, 1, 6);
    	
    	grid.add(proxyType, 1, 7);
    	
    	grid.add(vbox, 1, 8);
    	if("代理类型".equals(history.getProxyType())||"".equals(history.getProxyType())) {
    		proxyType.getSelectionModel().select("代理类型");
    		vbox.setDisable(true);
    	}else {
    		proxyType.getSelectionModel().select(history.getProxyType());
    	}
    	
    	
    	proxyType.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent event) {
				// TODO Auto-generated method stub
				if(proxyType.getSelectionModel().getSelectedIndex()==0) {
					
					
					
					vbox.setDisable(true);
				}else {
					vbox.setDisable(false);
				}
				
			}
		});

    	HBox panel = new HBox(80);
    	panel.setAlignment(Pos.BOTTOM_RIGHT);
    	panel.getChildren().addAll(confirm,cancle);
    	grid.add(panel, 1, 12);
    	
    	
    	pkey.setOnAction(e->{
    		 File file = fileChooser.showOpenDialog(null);
    		 fileChooser.setTitle("选择密钥");
    		 if (file != null) {
    			 pkey.setText(file.getPath());
             }else {
            	 pkey.setText("选择密钥");
             }
    		

    	});
    	cancle.setOnAction(e->{
    		Stage stage = (Stage)cancle.getScene().getWindow();
    		stage.close();
    	});
    	
    	confirm.setOnAction(e->{
    		
    		String pk="";
    		if(pkey.getText()!="选择密钥") {
    			pk=pkey.getText();
    		}
    	
    		
    		
    	History fhistory=new History(history.getId(), name.getText(), host.getText(), Integer.parseInt(port.getText()),
    				username.getText(), password.getText(), pk,proxyType.getValue(),proxyIp.getText(),proxyPort.getText(),
    				proxyUser.getText(),proxyPassword.getText(),iconimage);
    		

    		SqliteUtil sq=new SqliteUtil();
    		Stage stage = (Stage)confirm.getScene().getWindow();
    		 
				sq.update("history",fhistory);
				stage.close();
				 
    		
    		
    	});
    	
    	

    	return grid;
	
	}


	
	

}
